package com.gzhryc.shared_device.oem.code.config.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.JdbcSession;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.agent.services.AgentFuncModelService;
import com.gzhryc.shared_device.oem.code.agent.services.AgentRelationFuncModelService;
import com.gzhryc.shared_device.oem.code.config.dao.FuncModelDao;
import com.gzhryc.shared_device.oem.code.config.dao.db.FuncModel;
import com.gzhryc.shared_device.oem.code.config.services.dto.FuncModelSearch;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class FuncModelService {

    static Logger log = Logger.getLogger(FuncModelService.class);

    public static FuncModelService self() {
        return MultiDBTools.getService(FuncModelService.class, true);
    }

    public static FuncModelService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, FuncModelService.class, true);
    }

    FuncModelDao dao;

    public FuncModelService(String jdbcKey) {
        this.dao = new FuncModelDao(jdbcKey);
    }

    public List<FuncModel> findBySearch(FuncModelSearch search, Integer pageNo, Integer pageLimit){
        Conditions conditions = dao.buildConditions(search);
        conditions.desc("name");
        try {
            return dao.findByCondition(conditions,pageNo,pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public Long countBySearch(FuncModelSearch search){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.count(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public List<FuncModel> findByTypeAndState(Integer type,Integer state){
        if(type != null && state != null){
            Conditions conditions = new Conditions(FuncModel.class);
            conditions.and().is("type",type);
            conditions.and().is("state",state);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public List<FuncModel> findByType(Integer type){
        if(type != null){
            Conditions conditions = new Conditions(FuncModel.class);
            conditions.and().is("type",type);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public List<FuncModel> findByTypes(List<Integer> types){
        if(types != null && types.size() > 0){
            Conditions conditions = new Conditions(FuncModel.class);
            conditions.and().in("type",types);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public List<FuncModel> findByIds(List<Long> ids){
        if(ids != null && ids.size() > 0){
            Conditions conditions = new Conditions(FuncModel.class);
            conditions.and().in("id",ids);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public FuncModel getById(Long id){
        try {
            return dao.getById(id);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }
    
    public FuncModel getByKeys(Integer type,String url) {
    	if(type != null && StringTools.isNotBlank(url)) {
    		Conditions conditions = new Conditions(FuncModel.class);
            conditions.and().is("type",type);
            conditions.and().is("url",url);

            try {
                return dao.get(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
    	}
    	return null;
    }

    public boolean add(FuncModel funcModel) throws LogicException{
    	FuncModel temp = getByKeys(funcModel.getType(),funcModel.getUrl());
    	if(temp != null) {
    		throw new LogicException(funcModel.getTypeName() + "已存在" + funcModel.getUrl() + "功能模块");
    	}
        funcModel.setCreateDate(new Date());
        try {
            return dao.insert(funcModel) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean updateNotNull(FuncModel funcModel) throws LogicException{
    	FuncModel temp = getByKeys(funcModel.getType(),funcModel.getUrl());
    	if(temp != null && !temp.getId().equals(funcModel.getId())) {
    		throw new LogicException(funcModel.getTypeName() + "已存在" + funcModel.getUrl() + "功能模块");
    	}
        try {
            return dao.updateNotNull(funcModel) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean deleteById(Long id){
        JdbcSession.begin();
        try {
            if(dao.deleteById(id) > 0){
                if(AgentFuncModelService.self().deleteByFuncModelId(id)) {
                    if (AgentRelationFuncModelService.self().deleteByFuncModelId(id)) {
                        JdbcSession.commit();
                        return true;
                    }
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }finally {
            JdbcSession.end();
        }
        return false;
    }
}
